var resource = null;

function setLoading() {  
    var loadings = document.getElementById('loadings')
    loadings.style.display = 'block'
}

function removeLoading() {  
    var loadings = document.getElementById('loadings')
    loadings.style.display = 'none'
}

function getResource()
{
	return resource;
}

function initPage()
{
	var spanArray = document.getElementsByTagName("span");
	for(var i = 0; i < spanArray.length; i++)
	{
		var key = spanArray[i].getAttribute("local_key");
		if(key && key!="")
		{
		  spanArray[i].innerHTML = getResource()[key];
		}
	}
}

function load()
{
	setLoading();
	window.AppJsBridge.service.localeService.getResource({
		"success":function(data){
						resource = data;
						window.AppJsBridge.ready(function(){});
						initPage();
						queryState();
					},
		"error":function(data){
			removeLoading();
		}
	});

}

function getCurrentDeviceSn()
{
	
	return window.AppJsBridge.service.deviceService.getCurrentDeviceSn();
}

function queryState(){

	window.AppJsBridge.service.deviceService.doAction({
		"sn":getCurrentDeviceSn(),
		"action":"Status",
		"parameters":{"Temp":0},
		"deviceClass":"generalSwitch",
		"success":queryStateSucCallback,
		"error":queryStateFailCallback});

}

var queryStateSucCallback = function(data){
	
	removeLoading(); 
	if(data==null || data.result.result=="Fail")
    {
		document.getElementById("AIRMODE").innerHTML = "";
		document.getElementById("TEMP").innerHTML = "";
		document.getElementById("SPEED").setAttribute("src","Img/GJ.png");	
		document.getElementById("SWING").setAttribute("src","Img/GJ.png"); 
    } 
	else
    {
		
		var jsonObj = data.result;
		if(jsonObj.OperationStatus=="Close")
		{
			document.getElementById("AIRMODE").innerHTML = "";
			document.getElementById("TEMP").innerHTML = "";
			document.getElementById("SPEED").setAttribute("src","Img/GJ.png");	
			document.getElementById("SWING").setAttribute("src","Img/GJ.png"); 
			return;
		}
		if(jsonObj.Operationmode=="Auto")
		{
			document.getElementById("AIRMODE").innerHTML = getResource()["SAMSUNG_AIR_MODE_AUTO"];
		}
		else if(jsonObj.Operationmode=="Cooling")
		{
			document.getElementById("AIRMODE").innerHTML = getResource()["SAMSUNG_AIR_MODE_COOL"];
		}
		else if(jsonObj.Operationmode=="Dry")
		{
			document.getElementById("AIRMODE").innerHTML = getResource()["SAMSUNG_AIR_MODE_DRY"];
		}
		else if(jsonObj.Operationmode=="Fan")
		{
			document.getElementById("AIRMODE").innerHTML = getResource()["SAMSUNG_AIR_MODE_FAN"];
		}
		else if(jsonObj.Operationmode=="Heating")
		{
			document.getElementById("AIRMODE").innerHTML = getResource()["SAMSUNG_AIR_MODE_HEATING"];
		}
		
		if(jsonObj.Airflow=="flowAuto")
		{
			document.getElementById("SPEED").setAttribute("src","Img/GJ.png");	
		}
		else if(jsonObj.Airflow=="flowLow")
		{
			document.getElementById("SPEED").setAttribute("src","Img/FS.png");	
		}
		else if(jsonObj.Airflow=="flowMid")
		{
			document.getElementById("SPEED").setAttribute("src","Img/FZ.png");	
		}
		else if(jsonObj.Airflow=="flowHigh")
		{
			document.getElementById("SPEED").setAttribute("src","Img/FD.png");	
		}
		
		if(jsonObj.Swing=="swingOff")
		{
			document.getElementById("SWING").setAttribute("src","Img/GJ.png");	
		}
		else
		{
			document.getElementById("SWING").setAttribute("src","Img/SF.png");	
		}
		
		document.getElementById("TEMP").innerHTML = Math.floor(jsonObj.Temperature / 10) ; 
    }
	
}

var queryStateFailCallback = function(data)
{
	removeLoading();
	document.getElementById("AIRMODE").innerHTML = "";
	document.getElementById("TEMP").innerHTML = "";
	document.getElementById("SPEED").setAttribute("src","Img/GJ.png");	
	document.getElementById("SWING").setAttribute("src","Img/GJ.png");	
	
}

/*
 * 控制门阀的打开和关闭
 */
function changeSwitch()
{
	var action  = null;	
	setLoading();
	if(document.getElementById("AIRMODE").innerHTML == "")
	{
		action = "Open";
	}
	else
	{
		action = "Close";
	}
	
    if(action != null)
	{
		var sn = getCurrentDeviceSn();
		//可以运行到这来  但不能运行doAction  不进入success: switchSucCallback 也不进入 error 
    	window.AppJsBridge.service.deviceService.doAction({
    		"sn":sn, 
    		"action":action,
    		"parameters":{"Temp":0},
    		"deviceClass":"generalSwitch",
    		"success":switchSucCallback,
    		"error":function(data)
    		{
    			removeLoading();
			}
    	});
    }
    else
    {
    	removeLoading();
    }
}

/**
* 正常返回结果
* {"Status":"0","errorMessage":"","errorCode":0,"CmdType":"deviceManager.doAction","SequenceId":"0x00000564"}，
* 异常返回结果 其中-1表示超时。
* {"errCode":"-1"}
*/
 var switchSucCallback = function(data)
 {

	
	
	if(data==null || data.result.result=="Fail")
    {
		document.getElementById("AIRMODE").innerHTML = "";
		document.getElementById("TEMP").innerHTML = "";
		document.getElementById("SPEED").setAttribute("src","Img/GJ.png");	
		document.getElementById("SWING").setAttribute("src","Img/GJ.png"); 
		removeLoading();
		return;
    }
	var jsonObj = data.result;
	
	 
	if(jsonObj!=null && jsonObj.result!=null && jsonObj.result=="Success" && jsonObj.Status!=null && jsonObj.Status=="Close")
	{
		document.getElementById("AIRMODE").innerHTML = "";
		document.getElementById("TEMP").innerHTML = "";
		document.getElementById("SPEED").setAttribute("src","Img/GJ.png");	
		document.getElementById("SWING").setAttribute("src","Img/GJ.png"); 
	}
	else if(jsonObj!=null && jsonObj.result!=null  && jsonObj.result=="Success" && jsonObj.OperationStatus!=null)
	{
		if(jsonObj.OperationStatus=="Close")
		{
			document.getElementById("AIRMODE").innerHTML = "";
			document.getElementById("TEMP").innerHTML = "";
			document.getElementById("SPEED").setAttribute("src","Img/GJ.png");	
			document.getElementById("SWING").setAttribute("src","Img/GJ.png"); 
		}
		else
		{
			if(jsonObj.Operationmode=="Auto")
			{
				document.getElementById("AIRMODE").innerHTML = getResource()["SAMSUNG_AIR_MODE_AUTO"];
			}
			else if(jsonObj.Operationmode=="Cooling")
			{
				document.getElementById("AIRMODE").innerHTML = getResource()["SAMSUNG_AIR_MODE_COOL"];
			}
			else if(jsonObj.Operationmode=="Dry")
			{
				document.getElementById("AIRMODE").innerHTML = getResource()["SAMSUNG_AIR_MODE_DRY"];
			}
			else if(jsonObj.Operationmode=="Fan")
			{
				document.getElementById("AIRMODE").innerHTML = getResource()["SAMSUNG_AIR_MODE_FAN"];
			}
			else if(jsonObj.Operationmode=="Heating")
			{
				document.getElementById("Heating").innerHTML = getResource()["SAMSUNG_AIR_MODE_HEATING"];
			}
			
			if(jsonObj.Airflow=="flowAuto")
			{
				document.getElementById("SPEED").setAttribute("src","Img/GJ.png");	
			}
			else if(jsonObj.Airflow=="flowLow")
			{
				document.getElementById("SPEED").setAttribute("src","Img/FS.png");	
			}
			else if(jsonObj.Airflow=="flowMid")
			{
				document.getElementById("SPEED").setAttribute("src","Img/FZ.png");	
			}
			else if(jsonObj.Airflow=="flowHigh")
			{
				document.getElementById("SPEED").setAttribute("src","Img/FD.png");	
			}
			
			if(jsonObj.Swing=="swingOff")
			{
				document.getElementById("SWING").setAttribute("src","Img/GJ.png");	
			}
			else
			{
				document.getElementById("SWING").setAttribute("src","Img/SF.png");	
			}
			document.getElementById("TEMP").innerHTML = Math.floor(jsonObj.Temperature /10) ; 
		}
	}
	removeLoading();
 }
 
 function changetemp(node)
 {
	setLoading();
 	var action  = "Temp";
 	var tmp = 26;
 	if(document.getElementById("TEMP").innerHTML=="")
 	{
 		tmp = 26;
 	}
 	else
    { 
 		tmp = parseInt(document.getElementById("TEMP").innerHTML,10) ;
    }
 	
 	if("Add"==node)
 	{
 		tmp = tmp + 1;
 		if(tmp>40)
 		{
 			tmp = 40;
 		}
 	} 
 	else
 	{
 		tmp = tmp - 1;
 		if(tmp<=0)
 		{
 			tmp = 0;
 		}
 	}
 	
    if(action != null)
 	{
 		var sn = getCurrentDeviceSn();
     	window.AppJsBridge.service.deviceService.doAction({
     		"sn":sn, 
     		"action":action,
     		"parameters":{"Temp":tmp},
     		"deviceClass":"generalSwitch",
     		"success":tempCallback,
     		"error":function(data)
     		{
     			//alert(getResource().SWITCH_TOGGLE_FAILED);
 			}
     	});
     }
     else
     {
    	removeLoading();
     }
 }
 var tempCallback = function(data)
 {
	 var jsonObj = data.result;
	 if(jsonObj==null || jsonObj.result=="Fail")
	 {
		 removeLoading();
		 return;
	 }	 
	 else
     {
		 document.getElementById("TEMP").innerHTML = jsonObj.Temperature; 
		 removeLoading();
     }
 }
 function aircontrl(node)
 {
	    setLoading();
	    var action  = null;	
	 	if(document.getElementById("TEMP").innerHTML=="")
	 	{
	 		return;
	 	}
	 	var tmp = parseInt(document.getElementById("TEMP").innerHTML,10) ;
	 	if(node=="Mode")
	 	{
	 		if(document.getElementById("AIRMODE").innerHTML=="")
	 		{ 
	 			action = "Auto"
	 		}
	 		else if(document.getElementById("AIRMODE").innerHTML==getResource()["SAMSUNG_AIR_MODE_AUTO"])
	 		{ 
	 			action = "Cooling"
	 		}
	 		else if(document.getElementById("AIRMODE").innerHTML==getResource()["SAMSUNG_AIR_MODE_COOL"])
	 		{ 
	 			action = "Dry"
	 		}
	 		else if(document.getElementById("AIRMODE").innerHTML==getResource()["SAMSUNG_AIR_MODE_DRY"])
	 		{ 
	 			action = "Fan"
	 		}
	 		else if(document.getElementById("AIRMODE").innerHTML==getResource()["SAMSUNG_AIR_MODE_FAN"])
	 		{ 
	 			action = "Heating"
	 		}
	 		else if(document.getElementById("AIRMODE").innerHTML==getResource()["SAMSUNG_AIR_MODE_HEATING"])
	 		{ 
	 			action = "Cooling"
	 		}
	 	} 
	 	else if (node=="Airflow")
	 	{
	 		 if(document.getElementById("SPEED").getAttribute("src")=="Img/GJ.png")
	 		 {
	 			action = "flowLow"
	 		 }
	 		 else if(document.getElementById("SPEED").getAttribute("src")=="Img/FS.png")
	 		 {
		 		action = "flowMid"
		 	 }
	 		 else if(document.getElementById("SPEED").getAttribute("src")=="Img/FZ.png")
	 		 {
		 		action = "flowHigh"
		 	 }
	 		 else if(document.getElementById("SPEED").getAttribute("src")=="Img/FD.png")
	 		 {
		 		action = "flowAuto"
		 	 }
	 	}
	 	else if (node=="Swing")
	 	{
	 		 if(document.getElementById("SWING").getAttribute("src")=="Img/SF.png")
	 		 {
	 			action = "swingOff"
	 		 }
	 		 else  
	 		 {
		 		action = "swingOn"
		 	 } 
	 	}
	 	
	    if(action != null)
	 	{
	 		var sn = getCurrentDeviceSn();
	     	window.AppJsBridge.service.deviceService.doAction({
	     		"sn":sn, 
	     		"action":action,
	     		"parameters":{"Temp":tmp},
	     		"deviceClass":"generalSwitch",
	     		"success":controlCallback,
	     		"error":function(data)
	     		{
	     			removeLoading();
	 			}
	     	});
	     }
	     else
	     {
	    	 removeLoading();
	     }
 }
 var controlCallback = function(data)
 {
	 var jsonObj = data.result;
	 //alert(JSON.stringify(jsonObj));
	 if(jsonObj==null || jsonObj.result=="Fail")
	 {
		 removeLoading();
		 return;
	 }	 
	 else
     {
		 if(jsonObj.action!=null)
	     {
			    if(jsonObj.action=="Auto")
				{
					document.getElementById("AIRMODE").innerHTML = getResource()["SAMSUNG_AIR_MODE_AUTO"];
				}
				else if(jsonObj.action=="Cooling")
				{
					document.getElementById("AIRMODE").innerHTML = getResource()["SAMSUNG_AIR_MODE_COOL"];
				}
				else if(jsonObj.action=="Dry")
				{
					document.getElementById("AIRMODE").innerHTML = getResource()["SAMSUNG_AIR_MODE_DRY"];
				}
				else if(jsonObj.action=="Fan")
				{
					document.getElementById("AIRMODE").innerHTML = getResource()["SAMSUNG_AIR_MODE_FAN"];
				} 
				else if(jsonObj.action=="Heating")
				{
					document.getElementById("AIRMODE").innerHTML = getResource()["SAMSUNG_AIR_MODE_HEATING"];
				}
				else if(jsonObj.action=="flowAuto")
				{
					document.getElementById("SPEED").setAttribute("src","Img/GJ.png");	
				}
				else if(jsonObj.action=="flowLow")
				{
					document.getElementById("SPEED").setAttribute("src","Img/FS.png");	
				}
				else if(jsonObj.action=="flowMid")
				{
					document.getElementById("SPEED").setAttribute("src","Img/FZ.png");	
				}
				else if(jsonObj.action=="flowHigh")
				{
					document.getElementById("SPEED").setAttribute("src","Img/FD.png");	
				} 
				else if(jsonObj.action=="swingOff")
				{
					document.getElementById("SWING").setAttribute("src","Img/GJ.png");	
				}
				else if(jsonObj.action=="swingOn")
				{
					document.getElementById("SWING").setAttribute("src","Img/SF.png");	
				}
	     }
		 removeLoading();
     }
 }